﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;

using System.Collections.Generic;
using ApplicationGenerator.Versions.BusinessLogic;

//using ApplicationGenerator.Versions.BusinessLogic;


/// <summary>
/// Summary description for WebWorkflowManager
/// </summary>
public class WebWorkflowManager
{

    #region Variables

    private static WebWorkflowManager instance = null;
    private static object syncObj = new Object();
    private static object syncObj_creation = new Object();
    private static object syncObj_goAhead = new Object();
    private static object syncObj_showPage = new Object();
    private HttpResponse response = null;
    
    private static WCFS.ProcessService service = new WCFS.ProcessService();
    private static Dictionary<int, Guid> guids = new Dictionary<int, Guid>();
    


    #endregion

    #region Properties

    public HttpResponse Response
    {
        get { return response; }
        set { response = value; }
    }

    #endregion

    #region Construction

    private WebWorkflowManager()
    {
    }

    

    #endregion

    #region Contract Methods

    public void goAhead(Guid _workflowType, int _instanceId)
    {
        lock (syncObj_creation)
        {
            service.GoAhead(_workflowType,_instanceId);
            ShowPage(_workflowType, _instanceId);
        }
    }

    public Guid CreateWorkflow(Guid _id, int _instanceId)
    {
        lock (syncObj_creation)
        {
            Guid d = Guid.Empty;
            try
            {
                d = service.CreateWorkflow(_id, _instanceId);
                guids.Add(_instanceId, _id);            //<-- tego nie przepisuj to jest na potrzeby przykladu
            }
            catch (WCFS.WCFServiceException ex)
            {
                Response.Redirect("ErrorHandler.aspx?error_msg="+ex.Message);
            }

            return d;
        }
    }

    public void ShowPage( Guid _id, int _instanceId )
    {
        //start asking for new page to display..
        while (true)
        {
            Thread.Sleep(1000);

            if (WCFS.PROCESS_STATE.PROCESS_TO_BE_STOPPED == service.IsNewPageToDisplay(_id, _instanceId))
            {
                Response.Redirect("Default2.aspx");
            }

            if (WCFS.PROCESS_STATE.PROCESS_READY == service.IsNewPageToDisplay(_id,_instanceId))
            {
                VersionInfo sss = service.GetPageToDisplay(_id, _instanceId);
                
                if (sss.Name == "form1")
                {
                    Response.Redirect("Page1.aspx");
                }
                else if (sss.Name == "form2")
                {
                    Response.Redirect("Page2.aspx");
                }
                else if (sss.Name == "form3")
                {
                    Response.Redirect("Page3.aspx");
                }
                else if (sss.Name == "form4")
                {
                    Response.Redirect("Page4.aspx");
                }
                else if (sss.Name == "form5")
                {
                    Response.Redirect("Page5.aspx");
                }
                

                break;
            }
            else
            {
                //nothing to be displayed
            }

        }
    }

    public void ShowPageNoPreconditions(Guid _id, int _instanceId)
    {
        try
        {
            //start asking for new page to display..
            //while (true)
            {
                //Thread.Sleep(1000);

                //if (service.IsNewPageToDisplay(_instanceId))
                {
                    VersionInfo sss = service.GetPageToDisplay(_id, _instanceId);

                    if (sss.Name == "form1")
                    {
                        Response.Redirect("Page1.aspx");
                    }
                    else if (sss.Name == "form2")
                    {
                        Response.Redirect("Page2.aspx");
                    }
                    else if (sss.Name == "form3")
                    {
                        Response.Redirect("Page3.aspx");
                    }
                    else if (sss.Name == "form4")
                    {
                        Response.Redirect("Page4.aspx");
                    }
                    else if (sss.Name == "form5")
                    {
                        Response.Redirect("Page5.aspx");
                    }

                    // break;
                }
                //else
                {
                    //nothing to be displayed
                }

            }
        }
        catch (WCFS.WCFServiceException ex)
        {
            Response.Redirect("ErrorHandler.aspx?error_msg=" + ex.Message);
        }

    }


    /// <summary>
    /// This method is reposible for retrieving (or creating) single instance of this class.
    /// </summary>
    /// <param name="response"></param>
    /// <returns></returns>
    public static WebWorkflowManager getInstance(HttpResponse response)
    {
        lock (syncObj)
        {
            if (instance == null)
            {
                instance = new WebWorkflowManager();
                instance.Response = response ;
                return instance;
            }
            else
            {
                instance.Response = response;
                return instance;
            }
        }
    }

    #endregion

    #region metody do przykladu

    public Guid getGuidFor(int instanceId)
    {
        return guids[instanceId];
    }

    #endregion

}
